﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Gakita.Controller
{
    /* This controller deals with controlling the "start screen", "inbetween level screen" and the "End Screen", and handling the input             required for said screens */
    class InfoController : IController
    {
        public enum State
        {
           StartScreen,
           InBetweenLevelScreen,
           EndScreen
        }

        private MasterController.Controllers _nextController        =       MasterController.Controllers.Menu;       
        private bool _changeController                              =       false;
        private View.Menu.HighScoreModel _highscoreModel;
        private View.InfoView _infoView;
        private State _currentstate                                 =       State.StartScreen;

        public InfoController(GraphicsDeviceManager aGraphics, ContentManager aContentManager, SpriteBatch aSpriteBatch,                                                   View.Menu.HighScoreModel aHighScoreModel)
        {
            _highscoreModel = aHighScoreModel;
            _infoView = new View.InfoView(aGraphics, aContentManager, aSpriteBatch);
        }

        /* This method draws either the startscreen, the inbetween level screen or the end screen, depending on 
           which state is currently active */
        public void Draw(float aElapsedTime, GraphicsDevice aGraphicsDevice)
        {
            if (_currentstate == State.StartScreen)
            {
                _infoView.DrawStartScreen(aGraphicsDevice);
            }
            else if (_currentstate == State.InBetweenLevelScreen)
            {
                _infoView.DrawBetweenLevelsScreen(aGraphicsDevice,_highscoreModel.GetTime(),                                                                                                    _highscoreModel.GetRank(),_highscoreModel.GetLevel());
            }
            else if (_currentstate == State.EndScreen)
            {
                _infoView.DrawEndScreen(aGraphicsDevice);
            }
        }

        public void Update(float aElapsedTime)
        {
            // At the start screen, the user is forced to enter a username. 
            // Once a non-empty username have been entered, the highscoremodel saves it to use it when uploading highscores, 
            // and the MenuController becomes active 
            if(_currentstate == State.StartScreen)
            {
                if (_infoView.IsUserDoneWithNameInput())
                {
                    String username = _infoView.GetNameInput();
                    if (username.Length == 0)
                    {
                       // An empty username is not allowed, so nothing happens
                    }
                    else
                    {
                        _highscoreModel.SetUsername(username);
                        SetNextController(MasterController.Controllers.Menu);
                        _changeController = true;

                        // Since the startscreen only happens once (right at the start of the game), this controller switches state to 
                        // the Inbetween level screen, in anticipation of the user completing a level 
                        // and this controller becoming active again
                        _currentstate = State.InBetweenLevelScreen;
                    }
                }
            }

            // At the inbetween level screen, the user can press enter to continue to the next level, or return to the main menu,
            // depending on what game mode the user is playing. The controller changes once the user presses enter. If the user is 
            // at the last level, the end screen is shown after the inbetween level screen.
            else if (_currentstate == State.InBetweenLevelScreen)
            {
                if (_infoView.DidUserPressContinue())
                {
                    if (Model.Model._isSelectLevelMode == true)
                    {
                        SetNextController(MasterController.Controllers.Menu);
                        _changeController = true;
                        Model.Model._isSelectLevelMode = false;
                    }
                    else if (_highscoreModel.GetLevel() == Model.Level.Levels.fourth)
                    {
                        _currentstate = State.EndScreen;
                    }
                    else
                    {
                        SetNextController(MasterController.Controllers.Game);
                        _changeController = true;
                    }
                }
            }
            // At the end screen, the user can press enter to return to the menu
            else if (_currentstate == State.EndScreen)
            {
                if (_infoView.DidUserPressContinue())
                {
                    SetNextController(MasterController.Controllers.Menu);
                    _changeController = true;
                    Model.Model._gameIsRunning = false;
                }
            }
        }

        /* This method checks if the controller needs to be changed */
        public bool DidUserChangeController()
        {
            bool ret = _changeController;
            _changeController = false;
            return ret;
        }

        public void SetNextController(MasterController.Controllers aController)
        {
            _nextController = aController;
        }

        public MasterController.Controllers ControllerToChangeTo()
        {
            return _nextController;
        }

    }
}
